package be.kuleuven.cs.mop.domain.model.impl;


import static org.junit.Assert.*;

import org.junit.*;

import be.kuleuven.cs.mop.TestEnvironment;
import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;



public class WorldTest {
	
	private static TestEnvironment ENV;
	
	private static ProjectImpl	project;
	private static ResourceImpl	resource;
	private static UserImpl		user;
	
	
	
	@Before
	public void setUp() throws Exception {
		ENV = TestEnvironment.newInstance();
		
		project		= new ProjectImpl(ENV.world, "valid project");
		resource	= new ResourceImpl(ENV.world, "RoomX",
				new ResourceTypeImpl(ENV.world, "Room"));
		user		= new UserImpl(ENV.world, "valid user", ENV.userTypes.get(1));
	}
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {}
	
	@AfterClass
	public static void tearDownAfterClass() throws Exception {}
	
	@After
	public void tearDown() throws Exception {}
	
	@Test
	public void testConstructorDefault() {
		final World world = new World();
		assertTrue(world.getProjects().isEmpty());
		assertTrue(world.getResources().isEmpty());
		assertTrue(world.getTasks().isEmpty());
		assertTrue(world.getUsers().isEmpty());
		assertTrue(world.getClock().getTime().getTimeInMillis()
				<= System.currentTimeMillis());
	}
	
	@Test(expected=NullPointerException.class)
	public void testConstructorInvalidNull() {
		new World(null, null, null, null, null, null, null);
	}
	
	@Test
	public void testConstructorValid() {
		new World(new Clock(), null, null, null, null, null, null);
	}
	
	@Test
	public void testAdd() throws TaskManagerException {
		ENV.world.add(project);
		ENV.world.add(resource);
		ENV.world.add(user);
		assertTrue(ENV.world.getProjects().contains(project));
		assertTrue(ENV.world.getResources().contains(resource));
		assertTrue(ENV.world.getUsers().contains(user));
		assertNotNull(ENV.world.getTasks());
	}
	
	@Test(expected=TaskManagerException.class)
	public void testAddInvalid() throws TaskManagerException {
		new UserImpl(ENV.world,		"identical",	ENV.userTypes.get(1));
		new UserImpl(ENV.world,	"identical",	ENV.userTypes.get(1));
	}
	
	@Test
	public void testRemove() {
		for (final ProjectImpl project : ENV.world.getProjects())
			ENV.world.remove(project);
		assertTrue(ENV.world.getProjects().isEmpty());
		
		for (final ResourceImpl resource : ENV.world.getResources())
			ENV.world.remove(resource);
		assertTrue(ENV.world.getResources().isEmpty());
		
		for (final UserImpl user : ENV.world.getUsers())
			ENV.world.remove(user);
		assertFalse(ENV.world.getUsers().contains(user));
	}
	
	@Test
	public void testValidation() {
		assertNotNull(ENV.world.toString());
		assertFalse(ENV.world.toString().isEmpty());
	}
	
}